import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/close_device_manger_entity.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DoorControlController extends GetxController {

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final popScaffoldKeyCar = GlobalKey<ScaffoldState>();

  /// 人员状态
  String? personStatus;

  /// 人员状态显示
  String? personStatusShow;


  /// 设备类型  1:人员闸机 2:车辆闸机
  String? deviceType;

  /// 设备类型显示
  String? deviceTypeShow;

  /// 进出口 0：进 1：出 3：进/出
  String? inOut;

  /// 进出口显示
  String? inOutShow;

  /// 搜索
  String? searchCarName;

  /// 筛选
  void screen() {
    popScaffoldKeyCar.currentState?.openEndDrawer();
  }

  // 查询设备详情接口
  Future<List<CloseDeviceMangerEntity>> checkDeviceDetails(int pageNum,int pageSize, {String? search}) async {
    List<CloseDeviceMangerEntity> list = [];
    final res = await HttpUtil().generateGetRequest<List<CloseDeviceMangerEntity>>(Interface.getEquipmentDetail,queryParameters: {
      'pageNum': pageNum,
      'pageSize': pageSize,
      'keywords': searchCarName,// 设备名称
      'equipmentClassification': '1' ,
      'equipmentStatus': personStatus,
      'equipmentType': deviceType,
      'inOrOut': inOut,
    }).execute();

    res.onSuccess((result) {
      if(result.data != null) {
        list = result.data!;
      }
    });
    return list;

  }

  // 修改设备状态接口
  Future changeDeviceStatus(CloseDeviceMangerEntity item) async {
    final res = await HttpUtil().generatePostRequest(Interface.updateEquipment, data: {
      'id': item.id,
      'whetherToInspect': item.whetherToInspect == 1 ? 0 : 1, // 闸机是否验证车牌，1：开启验证 0：关闭验证 默认开启
    }).execute();
    res.onSuccess((result) {
      if(item.whetherToInspect == 1) {
        AppToast.showSuccess('关闭成功');
      } else {
        AppToast.showSuccess('开启成功');
      }
      globalKey.currentState!.onRefresh();
    });
  }

  confirm() {
    popScaffoldKeyCar.currentState?.closeEndDrawer();
    globalKey.currentState!.onRefresh();
  }

  reset() {
    personStatus = null;
    personStatusShow = null;
    searchCarName = null;
    deviceType = null;
    deviceTypeShow = null;
    inOut = null;
    inOutShow = null;
    update();
  }

}
